// @Author miaoqing
// @Date 2024/03/28 15:15:00
// @Desc
package logic

import (
	"core/command"
	"core/component/router"
	"time"
)

type Player struct {
	router            *router.Router
	playerID          uint64
	idx               int32
	isReady           bool
	isOnline          bool
	loadingProgress   int32
	lastHeartbeatTime int64
	sendFrameCount    uint32
}

func newPlayer(id uint64, idx int32) *Player {
	p := &Player{
		playerID: id,
		idx:      idx,
		router:   router.NewRouter(),
	}
	p.registerHandler()
	return p
}

func (player *Player) createRoom() {

}

func (player *Player) Connect() {
	player.isOnline = true
	player.isReady = false
	player.lastHeartbeatTime = time.Now().Unix()
}

func (player *Player) IsOnline() bool {
	return player.isOnline
}

func (player *Player) RefreshHeartbeatTime() {
	player.lastHeartbeatTime = time.Now().Unix()
}

func (player *Player) GetLastHeartbeatTime() int64 {
	return player.lastHeartbeatTime
}

func (player *Player) SetSendFrameCount(c uint32) {
	player.sendFrameCount = c
}

func (player *Player) GetSendFrameCount() uint32 {
	return player.sendFrameCount
}

func (player *Player) sendMsg(cmd command.Command, msg interface{}) {
	if !player.IsOnline() {
		return
	}
	data, err := player.router.Marshal(uint16(cmd), msg)
	if err != nil {
		return
	}
	clientMgrGetMe().sendMsgToClient(player.playerID, data)
}

func (player *Player) cleanUp() {
	player.isReady = false
	player.isOnline = false
	player.router.Clear()
	player.router = nil
}
